Skip to content
This repository has been archived by the owner on Oct 28, 2021. It is now read-only.

SIMD #4201

Closed
wants to merge 51 commits into from
Closed

SIMD #4201

wants to merge 51 commits into from

Conversation

gcolvin
Copy link
Contributor

@gcolvin gcolvin commented Jun 26, 2017

This is a portable implementation of the ethereum/EIPs#616 SIMD proposal.

  • The proposal itself has had a lot of attention as a result of trying to implement it, and because the wasm simd proposal surfaced.
  • The SIMD code itself is all in the new VMSIMD.cpp. The rest is just hooking it up.
  • The new opcodes and their implementations are walled off from production use with an EIP_616 macro set false in Instructions.h.
  • Yesterday I gave up on the fairly clean design I had been using - defining stack slots as a union of u256 and the simd vectors - because so much code changed accessing union members that rebasing failed miserably. The code became harder to read as well.
  • Instead I leave u256 as the slot type, so no code expecting that has to change. I punch through that type only in VMSIMD.cpp to store raw arrays on the same memory otherwise used by the u256 stack slots.

@codecov-io
Copy link

codecov-io commented Jun 26, 2017

Codecov Report

Merging #4201 into develop will increase coverage by 0.19%.
The diff coverage is 100%.

@@             Coverage Diff             @@
##           develop    #4201      +/-   ##
===========================================
+ Coverage    67.35%   67.54%   +0.19%     
===========================================
  Files          302      302              
  Lines        23257    23363     +106     
===========================================
+ Hits         15664    15781     +117     
+ Misses        7593     7582      -11
Impacted Files Coverage Δ
libevmcore/Instruction.cpp 31.81% <ø> (ø) ⬆️
libevmcore/Instruction.h 100% <ø> (ø) ⬆️
test/tools/fuzzTesting/fuzzHelper.cpp 55.46% <ø> (+1.95%) ⬆️
libevmcore/EVMSchedule.h 100% <ø> (ø) ⬆️
libevm/VM.h 76.92% <ø> (ø) ⬆️
test/unittests/libdevcrypto/LibSnark.cpp 100% <100%> (ø) ⬆️
libevm/VMCalls.cpp 97.85% <100%> (+0.03%) ⬆️
libevm/VM.cpp 95.97% <100%> (+0.02%) ⬆️
libethash/internal.c 71.51% <0%> (-1.27%) ⬇️
libp2p/Common.cpp 52.8% <0%> (+0.79%) ⬆️
... and 4 more

@gcolvin
Copy link
Contributor Author

gcolvin commented Jun 26, 2017

@winsvega clang won't compile the std::array initializer in fuzzHelper, but gcc and vcc will.

/Users/travis/build/ethereum/cpp-ethereum/test/tools/fuzzTesting/fuzzHelper.cpp:31:2: error: suggest braces around initialization of subobject [-Werror,-Wmissing-braces]

C++11 requires extra braces, C++14 doesn't. So I added the silly braces. They didn't break gcc, will see soon if the heal clang.

@@ -14,8 +14,6 @@
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file VM.cpp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we agreed on keeping these @file directives in the files

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think (could be wrong) Pawel asked me to remove them. I took a lot out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

libevm/VM.cpp Outdated
@@ -115,6 +113,22 @@ void VM::adjustStack(unsigned _removed, unsigned _added)
#endif
}

void VM::sstoreGas()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe name it updateGasForSstore

libevm/VM.cpp Outdated
@@ -115,6 +113,22 @@ void VM::adjustStack(unsigned _removed, unsigned _added)
#endif
}

void VM::sstoreGas()
{
if (m_ext->staticCall)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better leave this check out of the function updating the gas, it't not related to gas in any way

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied the existing code in SSTORE exactly. Could move this one piece to SSTORE and to XSTORE?

Copy link
Contributor Author

@gcolvin gcolvin Jun 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I should, it's included as a preamble to lots of functions that way. Somebody slipped them in while I wasn't looking ;)

libevm/VM.cpp Outdated
ON_OP();
updateIOGas();

xmul(m_code[++m_PC]); ++m_PC;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better put each statement on the separate line for readability and easier debugging

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created one function for advancing the PC and fetching the simd type.

libevm/VM.cpp Outdated
ON_OP();
updateIOGas();

xoor(m_code[++m_PC]); ++m_PC;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's VM::xoor and VM::xxor , shoudn't the latter be called here?
Also where's the case for XOOR ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think xoor is a typo and only one of them is needed, thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong. Seems xor is now a keyword, so I called the function xoor, and it needs a case to call it too.


void VM::vtow(uint8_t _b, const u256& _in, u256& _out)
{
const uint8_t n = pow2N((_b) & 0xf), t = (_b) >> 4;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One declaration per line, according to coding standards

void VM::wtov(uint8_t _b, const u256& _in, u256& _out)

{
const uint8_t n = pow2N((_b) & 0xf), t = (_b) >> 4;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This calculation is duplicated all over the place, create a couple of helper functions to get number of elements and type

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea.

typedef uint32_t a32x8 [8];
typedef uint16_t a16x16[16];
typedef uint8_t a8x32 [32];
a64x4& v64x4 ( u256& _stack_item) { return (a64x4&) *(a64x4*) &_stack_item; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Declare these functions inline maybe?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. (Though compilers are pretty good now.)

case 1:
for (int j = n, v = 0, i = 0; 0 < n; ++i)
{
v |= p[--j];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p[--j] is uint8_t, so you bit-or it with lowest 8 bits of v and then on the next line you shift right 8 bits, loosing what you've just read from p ? What am I getting wrong?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like I pasted the mstore() code as a starting template and then failed to reverse the shifts. Thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

switch (t)
{
case 0:
for (int i = 0; i < n; ++i) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Braces on separate lines

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another bad habit. Messed up less often than usual in this code...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that one function looks to be the only place. A record!

@gumb0
Copy link
Member

gumb0 commented Jun 27, 2017

This is an impressive amount of work, I wouldn't mind someone helping with the review 😃 @chfast @chriseth @pirapira

@gumb0
Copy link
Member

gumb0 commented Jun 27, 2017

I haven't finished looking through all of the VMSIMD.cpp yet, will try to continue tomorrow.

So far I mostly have style-related comments and some clarifying questions.

@gcolvin
Copy link
Contributor Author

gcolvin commented Jun 27, 2017

Thanks @gumb0 Andrei

@gcolvin
Copy link
Contributor Author

gcolvin commented Jul 7, 2017

This branch is gitted to death. Work has moved to /pull/4233.

@gcolvin
Copy link
Contributor Author

gcolvin commented Jul 10, 2017

Trashed and closed. Work continues here - #4233

@gcolvin gcolvin closed this Jul 10, 2017
@gcolvin gcolvin deleted the samd branch August 14, 2017 03:03
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants